package com.app.jpush.service.impl;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import com.app.jpush.service.JPushService;
import com.app.sysmodule.exception.ServiceException;
import com.app.sysmodule.utils.PropertyUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by song on 2018/1/22.
 */
public class JPushServiceImpl implements JPushService {
    public JSONObject handleGroupcast(List<String> userList, List<String> companyList, String channel, String source, String display_type, String filter_type, String filter, String ticker, String title, String text, String handleType) throws ServiceException {
        String appket = PropertyUtils.getProperty("appket");
        String appMasterSecret = PropertyUtils.getProperty("appMasterSecret");
        Boolean apnsProduction = Boolean.parseBoolean(PropertyUtils.getProperty("apns_production"));
        JSONObject jsonObject = new JSONObject();
        int errorCode = 0;
        String errorMsg = null;
        try {
            ClientConfig config = ClientConfig.getInstance();
            config.setMaxRetryTimes(5);
            config.setConnectionTimeout(10 * 1000); // 10 seconds
            config.setSSLVersion("SSLv3");
            JPushClient jPushClient = new JPushClient(appMasterSecret, appket);
            PushResult result = null;
            if ("01".equals(handleType)) {
                PushPayload iosPayload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(source)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(text).setBadge(0).setSound("default").addExtra("from", "JPush").build()).build()).setMessage(Message.content(text)).setOptions(Options.newBuilder().setApnsProduction(apnsProduction).build()).build();
                result = jPushClient.sendPush(iosPayload);
            } else if ("02".equals(handleType)) {
                if(null != companyList && companyList.size()>0 && companyList.size()<20){
                    PushPayload iosPayload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(source)).setAudience(Audience.tag(companyList)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(text).setBadge(0).setSound("default").addExtra("from", "JPush").build()).build()).setMessage(Message.content(text)).setOptions(Options.newBuilder().setApnsProduction(apnsProduction).build()).build();
                    result = jPushClient.sendPush(iosPayload);
                }else {
                    errorCode = 400;
                    errorMsg = "暂不支持该类型";
                }

            } else if ("03".equals(handleType)) {
                if(null != userList && userList.size()>0 && userList.size()<20){
                    Set<String> set=new HashSet<String>();
                    for(String str : userList){
                        set.add(channel+"_"+str);
                    }
                    PushPayload iosPayload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(source)).setAudience(Audience.tag(set)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(text).setBadge(0).setSound("default").addExtra("from", "JPush").build()).build()).setMessage(Message.content(text)).setOptions(Options.newBuilder().setApnsProduction(apnsProduction).build()).build();
                    result = jPushClient.sendPush(iosPayload);
                }else {
                    errorCode = 400;
                    errorMsg = "暂不支持该类型";
                }
            } else {
                errorCode = 404;
                errorMsg = "暂不支持该类型";
            }
            if (null != result && result.getResponseCode() == 200) {
                jsonObject.put("msg_id",result.msg_id);
                jsonObject.put("sendno",result.sendno);
                jsonObject.put("statusCode",result.statusCode);
                return jsonObject;
            }else {
                errorCode = result.error.getCode();
                errorMsg = result.error.getMessage();
            }
            jsonObject.put("errorCode",errorCode);
            jsonObject.put("errorMsg",errorMsg);
        } catch (APIRequestException e) {
           jsonObject = JSONObject.parseObject(e.getLocalizedMessage());
           return jsonObject;
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonObject;
    }

}
